package cn.wit.summit.chwan.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.wit.summit.chwan.R;

/**
 * Created by Alan on 2018/4/16.
 */

public class CountdownView extends LinearLayout {

    private TextView countdownHour, countdownMinute, countdownSecond;
    private long start_time;
    private String H = "00";
    private String M = "00";
    private String S = "00";

    private Context context;
    private OnCountdownListener mOnCountdownListener = null;

    public CountdownView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public CountdownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        this.context = context;

        View rootView = LayoutInflater.from(context).inflate(R.layout.part_countdown_view, null);
        countdownHour = rootView.findViewById(R.id.countdownHour);
        countdownMinute = rootView.findViewById(R.id.countdownMinute);
        countdownSecond = rootView.findViewById(R.id.countdownSecond);

        this.removeAllViews();
        this.addView(rootView);
    }

    public void setNowTime(long disTime, OnCountdownListener mOnCountdownListener) {
        this.mOnCountdownListener = mOnCountdownListener;
        setNowTime(disTime);
    }

    public void setNowTime(long disTime) {
        start_time = disTime;

        handler.removeMessages(1);
        Message message = handler.obtainMessage(1);     // Message
        handler.sendMessageDelayed(message, 0);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){

        public void handleMessage(Message msg){         // handle message
            switch (msg.what) {
                case 1:
                    changeShowTime();
                    Message message = handler.obtainMessage(1);
                    handler.sendMessageDelayed(message, 1000);      // send message
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void changeShowTime() {

        getTime(start_time);

        countdownHour.setText(H);
        countdownMinute.setText(M);
        countdownSecond.setText(S);
        invalidate();
        start_time -= 1;
        if (this.mOnCountdownListener!=null) {
            this.mOnCountdownListener.onResult(start_time);
        }
    }

    //根据秒数转化为时分秒
    private void getTime(long second) {
        long h = 0;
        long d = 0;
        long s = 0;
        long temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }

        if (h > 0){
            if (h < 10)
                H = "0" + h;
            else
                H = "" + h;
        } else {
            H = "00";
        }

        if (d > 0) {
            if (d < 10)
                M = "0" + d;
            else
                M = "" + d;
        } else {
            M = "00";
        }

        if (s > 0) {
            if (s < 10)
                S = "0" + s;
            else
                S = "" + s;
        } else {
            S = "00";
        }
    }

    public interface OnCountdownListener {
        void onResult(long time);
    }
}
